package com.slm.report.view.data;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.slm.report.R;
import com.slm.report.databinding.ViewTextContentBinding;
import com.slm.report.db.SchoolDtaBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: My Application
 * @Package: com.slm.report.view.data
 * @ClassName: ChoiceContentView
 * @Description: java类作用描述
 * @Author: 王丹丹
 * @CreateDate: 2020/6/2 10:52
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/6/2 10:52
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class SchoolChoiceContentView extends BaseContentView {
    private ViewTextContentBinding viewTextContentBinding = null;
    private OptionsPickerView optionsPickerView;
    private List<SchoolDtaBean> schoolDtaBeans = new ArrayList<>();

    public SchoolChoiceContentView(Context context) {
        super(context);
        viewTextContentBinding = ViewTextContentBinding.inflate(LayoutInflater.from(getContext()), this, true);


    }

    @Override
    void upDataContent() {
        viewTextContentBinding.titleTv.setText(contentDataBean.getTitleStr());
        if (isEdit) {
            if (StringUtils.isEmpty(contentDataBean.getContent())) {
                viewTextContentBinding.contentTv.setText("请选择" + contentDataBean.getTitleStr());
            }else{
                viewTextContentBinding.contentTv.setText(contentDataBean.getContent());
            }

            viewTextContentBinding.contentTv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (optionsPickerView == null) {
                        schoolDtaBeans = SQLite.select()
                                .from(SchoolDtaBean.class)
                                .queryList();
                        if (schoolDtaBeans == null) {
                            schoolDtaBeans = new ArrayList<>();
                        }
                        initCustomOptionPicker();
                    }
                    optionsPickerView.show();
                }
            });
        } else {
            viewTextContentBinding.contentTv.setText(contentDataBean.getContent());
            viewTextContentBinding.contentTv.setOnClickListener(null);
        }

    }


    private void initCustomOptionPicker() {//条件选择器初始化，自定义布局
        optionsPickerView = new OptionsPickerBuilder(mContext, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                String tx = schoolDtaBeans.get(options1).getPickerViewText();
                viewTextContentBinding.contentTv.setText(tx);
                contentDataBean.setContent(tx);
                viewTextContentBinding.contentTv.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
            }
        })
                .setLayoutRes(R.layout.choice_view_layout, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        View addData = v.findViewById(R.id.add_data);
                        addData.setVisibility(VISIBLE);
                        final TextView tvSubmit = v.findViewById(R.id.tv_sure);
                        final TextView tvCancel = v.findViewById(R.id.tv_cancel);
                        final View chooseLl = v.findViewById(R.id.choose_ll);
                        final View addLl = v.findViewById(R.id.add_ll);
                        final View dataCancel = v.findViewById(R.id.data_cancel);
                        final View dataSave = v.findViewById(R.id.data_add);
                        final EditText editText = v.findViewById(R.id.editText);
                        tvSubmit.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                optionsPickerView.returnData();
                                optionsPickerView.dismiss();
                            }
                        });
                        addData.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                addLl.setVisibility(VISIBLE);
                                chooseLl.setVisibility(View.GONE);
                            }
                        });

                        dataCancel.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                addLl.setVisibility(GONE);
                                chooseLl.setVisibility(View.VISIBLE);
                                editText.setText("");
                            }
                        });
                        dataSave.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (StringUtils.isEmpty(editText.getText())) {
                                    ToastUtils.showShort("请输入学校名称");
                                    return;
                                }
                                SchoolDtaBean schoolDtaBean = new SchoolDtaBean();
                                schoolDtaBean.setName(editText.getText().toString());
                                schoolDtaBean.save();
                                schoolDtaBeans.add(schoolDtaBean);
                                optionsPickerView.setPicker(schoolDtaBeans);
                                addLl.setVisibility(GONE);
                                chooseLl.setVisibility(View.VISIBLE);
                                editText.setText("");
                            }
                        });

                        tvCancel.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                optionsPickerView.dismiss();
                            }
                        });
                    }
                })
                .isDialog(true)
                .setOutSideCancelable(false)
                .build();

        optionsPickerView.setPicker(schoolDtaBeans);


    }
}
